package com.my.algorithm;

import com.my.algorithm.entity.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 碳数据计算 工具类
 */

public class CarbonDeclareUtil {
//       自评-固定源
    public static List<SysFixedSource> fixedSources;
//    自评-移动源
    public static List<SysFactorLowFever> moveSources;
//    自评-碳酸盐
    public static List<SysFactor> carbonateSources;
    //自评-间接温室气体
    public static List<SysFactorLowFever>  indirectSources;
    public static void initGetData(Object fixed, Object move, Object carbonate, Object  indirect){
        //固定源
        fixedSources= (List<SysFixedSource>)fixed;
        moveSources=(List<SysFactorLowFever>)move;
        carbonateSources=(List<SysFactor>)carbonate;
        indirectSources=(List<SysFactorLowFever>)indirect;
    }

    public static Map<String,BigDecimal> computeDeclare(OtherProduce otherProduce){
//        if(fixedSources==null)
//            initGetData();
        if(otherProduce==null)
            return null;
        Map<String,BigDecimal> fix=computeFixedSource(otherProduce.getFixedfuels());
        Map<String,BigDecimal> move= computeMoveSource(otherProduce.getMovefuels());
        BigDecimal carbon= computeCarbonates(otherProduce.getCarbonates());
        Map<String,BigDecimal>  indirect=computeIndirect( otherProduce.getIndirects());
        Map<String,BigDecimal> map=new HashMap<>();
        BigDecimal energy=new BigDecimal(0),productNums=new BigDecimal(0);
        if(fix!=null){
            carbon=carbon.add(fix.get("carbon"));
            energy=energy.add(fix.get("energy"));
        }
        if(move!=null){
            carbon=carbon.add(move.get("carbon"));
            energy=energy.add(move.get("energy"));
        }
        if(indirect!=null){
            carbon=carbon.add(indirect.get("carbon"));
            energy=energy.add(indirect.get("energy"));
        }
        if(otherProduce.getProducts()!=null&&otherProduce.getProducts().size()>0){
            for( CarbonDeclareProduct product:otherProduce.getProducts()){
                productNums=productNums.add(product.getProductNum());
            }
            BigDecimal danCarbon=carbon.divide(productNums,4,BigDecimal.ROUND_HALF_UP);
            BigDecimal danEnergy=energy.divide(productNums,4,BigDecimal.ROUND_HALF_UP);
            map.put("danCarbon",danCarbon);
            map.put("danEnergy",danEnergy);
        }

        carbon=carbon.setScale(4,BigDecimal.ROUND_HALF_UP);
        energy=energy.setScale(4,BigDecimal.ROUND_HALF_UP);
        map.put("carbon",carbon);
        map.put("energy",energy);

        return map;
    }

    public static Map<String, BigDecimal> computeFixedSource(List<CarbonDeclareFixedfuel> fixedfuels){
        if(fixedfuels==null)
            return null;
        BigDecimal carbon=new BigDecimal(0),energy=new BigDecimal(0);
        for(CarbonDeclareFixedfuel fixedfuel:fixedfuels){
            for(SysFixedSource source:fixedSources){
                if(fixedfuel.getFuelType().equals(source.getName())&&fixedfuel.getUnit().equals(source.getUnit())){
                    carbon=carbon.add(source.getFactors().multiply(fixedfuel.getNum()));
                    energy=energy.add(source.getLowFever().multiply(fixedfuel.getNum()));
                    break;
                }
            }
        }
        Map<String, BigDecimal> map=new HashMap<>();
        map.put("carbon",carbon);
        map.put("energy",energy);
        return map;
    }
    public static Map<String, BigDecimal> computeMoveSource(List<CarbonDeclareMovefuel> movefuels){
        if(movefuels==null)
            return null;
        BigDecimal carbon=new BigDecimal(0),energy=new BigDecimal(0);
        for(CarbonDeclareMovefuel movefuel:movefuels){
            for(SysFactorLowFever source:moveSources){
                if(movefuel.getFuelType().equals(source.getName())&&movefuel.getUnit().equals(source.getUnit())){
                    carbon=carbon.add(source.getFactors().multiply(movefuel.getNum()));
                    energy=energy.add(source.getLowFever().multiply(movefuel.getNum()));
                    break;
                }
            }
        }
        Map<String, BigDecimal> map=new HashMap<>();
        map.put("carbon",carbon);
        map.put("energy",energy);
        return map;
    }
    public static BigDecimal computeCarbonates(List<CarbonDeclareCarbonate> carbonates){
        if(carbonates==null)
            return null;
        BigDecimal carbon=new BigDecimal(0);
        for(CarbonDeclareCarbonate movefuel:carbonates){
            for(SysFactor source:carbonateSources){
                if(movefuel.getCarbonateType().equals(source.getName())&&movefuel.getUnit().equals(source.getUnit())){
                    carbon=carbon.add(source.getFactors().multiply(movefuel.getNum()));
                    break;
                }
            }
        }
        return carbon;
    }
    public static Map<String, BigDecimal> computeIndirect( List<CarbonDeclareIndirect> indirects){
        if(indirects==null)
            return null;
        BigDecimal carbon=new BigDecimal(0),energy=new BigDecimal(0);
        for(CarbonDeclareIndirect movefuel:indirects){
            for(SysFactorLowFever source:indirectSources){
                if(movefuel.getType().equals(source.getName())&&movefuel.getUnit().equals(source.getUnit())){
                    carbon=carbon.add(source.getFactors().multiply(movefuel.getNum()));
                    energy=energy.add(source.getLowFever().multiply(movefuel.getNum()));
                    break;
                }
            }
        }
        Map<String, BigDecimal> map=new HashMap<>();
        map.put("carbon",carbon);
        map.put("energy",energy);
        return map;
    }


}
